package cn.sinata.wanglin.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Contact;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.ImageUtil;

/**
 *
 * Created by liaoxiang on 16/12/20.
 */

public class ContactsFriendsAdapter extends HFRecyclerAdapter<Contact> {
    private static final int TYPE_GROUP_TITLE = 10;
    private HashMap<String ,Integer> letters = new HashMap<>();
    private String userId;

    public ContactsFriendsAdapter(List<Contact> mData) {
        super(mData, R.layout.item_contact_friends);
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
    }

    public void setLetters(HashMap<String,Integer> letters) {
        this.letters = letters;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_GROUP_TITLE) {
            LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
            View view = mInflater.inflate(R.layout.item_contact_friends_group_layout, parent, false);
            return new ViewHolder(view);
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBind(int position, final Contact contact, ViewHolder holder) {
        int type = getItemViewType(hasHeader()?position+1:position);

        if (type == TYPE_GROUP_TITLE) {
            TextView tv_head = holder.bind(R.id.tv_head);
            tv_head.setText(contact.getFirstChar());
        }
        holder.setText(R.id.tv_name,contact.getContactName());
        holder.setText(R.id.tv_content,"网邻:"+contact.getName());
        SimpleDraweeView headView = holder.bind(R.id.headView);
        headView.setImageURI(ImageUtil.parseImageUri(contact.getHeadUrl()));
        TextView actionView = holder.bind(R.id.tv_action);
        actionView.setText(contact.getActionStr());

        final int isFriend = contact.getIsFriend();
        actionView.setSelected(isFriend == 0);
        actionView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isFriend == -1 || isFriend == 2) {
                    sendAuthMsg(contact);
                } else if (isFriend == 0){
                    acceptFriendAuth(contact);
                }

            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        int type = super.getItemViewType(position);
        if (type == TYPE_NORMAL) {
            int p = position;
            if (hasHeader()) {
                p = position-1;
            }
            Contact contact = mData.get(p);
            String firstChar = contact.getFirstChar();
            if (letters.containsKey(firstChar) && letters.get(firstChar) == p) {
                return TYPE_GROUP_TITLE;
            }
        }
        return super.getItemViewType(position);
    }

    private void sendAuthMsg(final Contact contact){
        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).showDialog();
        }
        String content = "来自通讯录";
        HttpManager.applyFriend(userId,contact.getFriendID(),content)
                .subscribe(new ResultDataSubscriber<JsonObject>((BaseActivity) mContext) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        //待接受。
                        contact.setIsFriend(3);
                        notifyDataSetChanged();
                    }
                });
    }

    private void acceptFriendAuth(final Contact contact){
        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).showDialog();
        }
        //状态（1：申请通过 2，申请不通过，3删除）
        HttpManager.disposeApplyFriend(userId,1,contact.getApplyId())
                .subscribe(new ResultDataSubscriber<JsonObject>((BaseActivity) mContext) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        //已接受。
                        contact.setIsFriend(1);
                        notifyDataSetChanged();
                    }
                });
    }
}
